package com.cloud.pet.ops.scheduler;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pet.api.manager.op.AlarmManager;
import com.cloud.pet.api.req.op.AlarmStatReq;

import cn.hutool.core.date.StopWatch;
import lombok.extern.log4j.Log4j2;

@RestController
@Component
@Log4j2
public class AlarmScheduler {

    @Resource
    private AlarmManager alarmManager;

    /**
     * 每过一小时 告警逾期更新
     */
    // 每天凌晨1点执行一次：0 0 1 * * ?
    // 每隔1分钟执行一次：0 */1 * * * ?
    @Scheduled(cron = "0 0 */1 * * ?")
    public void exceedTally() {
        log.info("服务点计算开始...");
        StopWatch sw = new StopWatch();
        try {
            sw.start("exceedTally");
            alarmManager.exceedTally(AlarmStatReq.builder().build());
            sw.stop();
            log.info("告警逾期更新完成，耗时{}ms", sw.getLastTaskTimeMillis());
        } catch (Exception e) {
            sw.stop();
            log.error("告警逾期更新异常，耗时{}ms，{}", sw.getLastTaskTimeMillis(), e.getMessage());
        }
    }
}